package com.zhlb.zfba.modules.card.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.zhlb.zfba.common.config.Global;
import com.zhlb.zfba.common.persistance.Msg;
import com.zhlb.zfba.modules.card.bean.CardRegInfo;
import com.zhlb.zfba.modules.card.service.CardRegInfoService;
import com.zhlb.zfba.modules.per.bean.PoliceInfo;
import com.zhlb.zfba.modules.per.bean.PolicePathLog;
import com.zhlb.zfba.modules.per.service.PoliceInfoService;
import com.zhlb.zfba.modules.per.service.PolicePathLogService;

@Controller
@RequestMapping(value = "/card/cardRegInfo")
public class CardRegInfoController {
	@Autowired
	private CardRegInfoService cardRegInfoService;
	@RequestMapping(value = {"getCardRegInfo"})
	@ResponseBody
	public CardRegInfo getCardRegInfo(CardRegInfo cardRegInfo) {
		CardRegInfo cardRegInfoEntity =cardRegInfoService.get(cardRegInfo.getCardID());
		return cardRegInfoEntity;
	}
	@RequestMapping(value = {"isExsitCardNo"})
	@ResponseBody
	public Msg isExsitCardNo(CardRegInfo cardRegInfo) {
		if(StringUtils.isNotBlank(cardRegInfo.getCardNo1())&&StringUtils.isNotBlank(cardRegInfo.getCardNo2())){
			return Msg.failure();
		}
		CardRegInfo cardRegInfoEntity =cardRegInfoService.getByCardNo(cardRegInfo);
		if(cardRegInfoEntity!=null&&StringUtils.isNotBlank(cardRegInfoEntity.getCardID()))
		{
			return Msg.success();
		}else{
			return Msg.failure();
		}
		
	}
	@RequestMapping(value = {"validateUpdateCardNo"})
	@ResponseBody
	public Msg validateUpdateCardNo(CardRegInfo cardRegInfo) {
		CardRegInfo cardRegInfoEntity = cardRegInfoService.getByCardNo(cardRegInfo);
		if (cardRegInfoEntity != null && StringUtils.isNotBlank(cardRegInfoEntity.getCardID())) {
			if (cardRegInfoEntity.getCardID().equals(cardRegInfo.getCardID())) {
				return Msg.failure();//修改自己的卡号，别的记录没这个卡号
			}
			return Msg.success();
		} else {
			return Msg.failure();
		}
		
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(CardRegInfo cardRegInfo,HttpServletRequest request, HttpServletResponse response, Model model) {
		// 引入PageHepler插件
		String pageSizeStr=Global.getConfig("pagesize");
		if (!StringUtils.isNumeric(pageSizeStr)){
			pageSizeStr="10";
		}
		Integer pageSize=Integer.parseInt(pageSizeStr);
		Integer pn=0;
		if (StringUtils.isNumeric(request.getParameter("pageNo"))){
			pn=Integer.parseInt(request.getParameter("pageNo"));
		}
		//
		PageHelper.startPage(pn, pageSize);
		List<CardRegInfo> list=cardRegInfoService.findList(cardRegInfo);
		PageInfo pageInfo = new PageInfo(list, 5);
		model.addAttribute("cardRegInfo", cardRegInfo);//查询条件回显
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("pageInfoJson", JSON.toJSONString(pageInfo));
			return "modules/card/cardRegInfoList";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Msg savePoliceInfo(CardRegInfo cardRegInfo, BindingResult result) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		if (result.hasErrors()) {
//			List<FieldError> errors = result.getFieldErrors();
//			for (FieldError error : errors) {
//				map.put(error.getField(), error.getDefaultMessage());
//
//			}
//			return Msg.failure().add("errorFileds", map);
//		}
		cardRegInfoService.save(cardRegInfo);
		return Msg.success();
	}
	
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(String ids) {
		cardRegInfoService.delete(ids);
		return "";
	}
}
